package com.city.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.city.eduservice.entity.EduChapter;
import com.city.eduservice.entity.EduVideo;
import com.city.eduservice.entity.vo.chapthe.ChapterVo;
import com.city.eduservice.entity.vo.chapthe.VideoVo;
import com.city.eduservice.mapper.EduChapterMapper;
import com.city.eduservice.service.EduChapterService;
import com.city.eduservice.service.EduVideoService;
import com.city.servicebase.myexception.MyException;
import com.city.servicebase.myexception.exceptionenum.MyExceptionEnumImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author city
 * @since 2020-07-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //先根据唯一课程对应id查出该课程得章节和小节，统一查出全部课程得章节和小节再通过java组合list返回
        //而不是查出章节再循环拿出小节，多次访问数据库不好

        //该课程的章节
        QueryWrapper<EduChapter> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(queryWrapper);

        //该课程的小节
        QueryWrapper<EduVideo> videoqueryWrapper = new QueryWrapper<>();
        videoqueryWrapper.eq("course_id",courseId);
        List<EduVideo> list = eduVideoService.list(videoqueryWrapper);

        //返回的已封装的数据
        List<ChapterVo> chapterVoList=new ArrayList<>();

        //遍历章节
        for (EduChapter eduChapter : eduChapters) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);

            //遍历小节，放入chapterVo
            List<VideoVo> videoVos=new ArrayList<>();
            for (EduVideo eduVideo : list) {
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo video = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,video);
                    videoVos.add(video);
                }
            }
            chapterVo.setChildren(videoVos);
            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }

    /**
     * 删除章节
     * 如果章节种有小节，则删除失败，如果没有则成功，如果有，必须先删除该章节的所有小节
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查对应的小节
        QueryWrapper<EduVideo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(queryWrapper);
        if (count>0){
            throw new MyException(MyExceptionEnumImpl.NOT_DELETE);
        }
        int i = baseMapper.deleteById(chapterId);
        return i>0;
    }
}